package com.szp.drug.component.controller;

import com.github.pagehelper.PageInfo;
import com.szp.drug.component.service.api.DrugMedicineService;
import com.szp.drug.entity.DrugMedicine;
import com.szp.drug.util.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 药品前端控制器
 * @author SuZePing
 * @create 2021-03-05 21:58
 * @E-mail suzeping10@126.com
 */
@Slf4j
@RestController
@RequestMapping("/medicine")
public class DrugMedicineController {

	@Autowired
	private DrugMedicineService medicineService;

	/**
	 * 分页获取药品
	 * @param medicineName
	 * @param medicineType
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/get/page.json")
	public ResultEntity<PageInfo<DrugMedicine>> getPageInfo(
			@RequestParam(value = "medicineName",defaultValue = "") String medicineName,
			@RequestParam(value = "medicineType",defaultValue = "") String medicineType,
			@RequestParam(value = "pageNum",defaultValue = "1") String pageNum,
			@RequestParam(value = "pageSize",defaultValue = "5") String pageSize
	){
		return medicineService.getPageInfo(medicineName, medicineType, pageNum, pageSize);
	}

	/**
	 * 新增药品(并返回新增后总数量)
	 * @param medicine
	 * @return
	 */
	@PostMapping("/save.json")
	public ResultEntity<Integer> saveMedicine(DrugMedicine medicine){
		return medicineService.saveMedicine(medicine);
	}

	/**
	 * 修改药品
	 * @param medicine
	 * @return
	 */
	@PostMapping("/edit.json")
	public ResultEntity<String> editMedicine(DrugMedicine medicine){
		return medicineService.editMedicine(medicine);
	}

	/**
	 * 删除药品
	 * @param medicineId
	 * @return
	 */
	@PostMapping("/del.json")
	public ResultEntity<String> removeMedicine(@RequestParam("medicineId") String medicineId){
		return medicineService.removeMedicine(medicineId);
	}
}